import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

import "@/assets/iconfont/iconfont";
import "@/assets/iconfont/iconfont.css";
import "@/assets/styles/iconfont.css";
Vue.config.productionTip = false;

import qfSubMenu from "qf-sub-menu";
Vue.use(qfSubMenu);

import "@/assets/styles/el-reset.css";

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

import nprogress from "nprogress";
import "nprogress/nprogress.css";

// 路由守卫
router.beforeEach((to, from, next) => {
  nprogress.start();
  if (localStorage.getItem("qf-token")) {
    if (store.state.userMenu.length === 0) {
      store.dispatch("FETCH_MENULIST").then(() => {
        // next(); //直接放形是无法执行 push的路径的，需要重新激活to的路径
        // console.log("to", { ...to });
        next({ path: to.path, replace: true }); //FETCH_MENULIST得 丢失  重新定向路由=>激活请求
      }); //replace  不产生历史记录
    } else {
      next();
    }
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next({ path: "/login" });
    }
  }
});

router.afterEach((to, from) => {
  nprogress.done();
  let matched = to.matched.filter((item, index) => index > 0);
  //   console.log(matched);
  store.commit("SET_CRUMB", matched);
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
